<template>
  <!-- 搜索角色框 -->
  <div class="search-section">
    <input type="text" class="search-input" placeholder="搜索想要对话的角色..." :value="modelValue" @input="handleInput"></input>
    <button class="search-btn" @click="clearSearch" v-show="modelValue.trim()">
      X
    </button>
  </div>
</template>

<script setup>
import { ref } from 'vue';
const props = defineProps({
  modelValue: {
    type: String,
    default: ''
  }
});

const emit = defineEmits(['update:modelValue', 'input']);

// 处理输入事件，同时更新父组件的值并触发搜索
const handleInput = (event) => {
  const value = event.target.value;
  console.log('搜索输入:', value); // 查看输入值
  
  // 更新父组件的v-model值
  emit('update:modelValue', value);
  
  // 触发父组件的搜索逻辑
  emit('input', value);
};

// 清空搜索，重置父组件的搜索值
const clearSearch = () => {
  console.log('清空搜索'); 
  emit('update:modelValue', '');
  emit('input', '');
};
</script>

<style scoped>
.search-section {
  max-width: 1000px;
  margin: 40px auto;
  padding: 0 25px;
  display: flex;
  position: relative;
}

.search-input {
  width: 80%;
  flex: 1;
  padding: 20px 60px 20px 25px;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.001), rgba(0, 0, 0, 0.001)), rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(4px);
  box-shadow: 0px 8px 10px -6px rgba(0, 0, 0, 0.1), 0px 20px 25px -5px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(74, 108, 247, 0.4);
  border-radius: 9999px;
  color: var(--text-light);
  font-size: 1.2rem;
  outline: none;
  transition: all 0.3s;
}

.search-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 20px rgba(74, 108, 247, 0.3);
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-btn {
  position: absolute;   
  right: 100px;
  top: 50%;           
  transform: translateY(-50%); 
  border: none;
  color: #666;
  cursor: pointer;
  background: none;
  font-size: 18px;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.3s;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-btn:hover {
  background: rgba(74, 108, 247, 0.1);
  color: var(--primary);
  transform: translateY(-50%) scale(1.1);
}

.search-section::before {
  content: "🔍";
  position: absolute;
  right: 50px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 18px;
  color: var(--text-muted);
  pointer-events: none;
  opacity: 0.7;
  z-index: 1;
}

/* 当有输入内容时隐藏搜索图标 */
.search-input:not(:placeholder-shown) + .search-btn::before,
.search-input:focus + .search-btn::before {
  display: none;
}
</style>
